package quick.frame.core.base;

import android.content.Context;
import android.widget.Toast;

import com.elvishew.xlog.XLog;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import quick.frame.core.exception.BaseException;
import quick.frame.core.util.NetworkConnectionUtils;

public abstract class BaseSubscriber<T> implements Subscriber<T> {

    private Context context;

    public BaseSubscriber(Context context) {
        this.context = context;
    }

    @Override
    public void onSubscribe(Subscription s) {

        XLog.i("tag","MySubscriber.onStart()");
        //接下来可以检查网络连接等操作
        if (!NetworkConnectionUtils.isConnected(context)) {

            Toast.makeText(context, "当前网络不可用，请检查网络情况", Toast.LENGTH_SHORT).show();
            // 一定好主动调用下面这一句,取消本次Subscriber订阅
            if (s != null) {
                s.cancel();
            }
            return;
        }
    }


    @Override
    public void onNext(T t) {

    }

    @Override
    public void onError(Throwable e) {
        XLog.e("tag","MySubscriber.throwable ="+e.toString());
        XLog.e("tag","MySubscriber.throwable ="+e.getMessage());

        if(e instanceof Exception){
            //访问获得对应的Exception
            onError(BaseException.handleException(e));
        }
    }

    @Override
    public void onComplete() {
        XLog.i("tag","MySubscriber.onComplete()");
    }

    public abstract void onError(BaseSubscriber responeThrowable);


}
